package com.ruoyi.common.core.annotation.impl;

import com.ruoyi.common.core.annotation.NonNullVerification;
import com.ruoyi.common.utils.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @author zgx
 * @Title:
 * @Package
 * @Description:
 * @date 2022/5/6 15:30
 */
public class NonNullVerificationValidator implements ConstraintValidator<NonNullVerification,String>{

    private String REGEXP = null;

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isNotEmpty(value)) {
            return Pattern.matches(REGEXP, value.trim());
        }
        return true;
    }

    @Override
    public void initialize(NonNullVerification constraintAnnotation) {
        REGEXP=constraintAnnotation.regexp();
    }
}
